local tunshi = fk.CreateSkill{
  name = "hanqing__tunshi",
}

Fk:loadTranslationTable{
  ["hanqing__tunshi"] = "吞世",
  [":hanqing__tunshi"] = "每回合限一次，当一名角色进入濒死状态时，你可以展示所有手牌并获得中央区手牌中缺失的花色的牌各一张，若你没有手牌，"..
  "则无须展示手牌。",

  ["#hanqing__tunshi"] = "吞世：获得每个花色的牌各一张",
}

local U = require "packages/utility/utility"

Fk:addPoxiMethod{
  name = "hanqing__tunshi",
  prompt = "#hanqing__tunshi",
  card_filter = Util.TrueFunc,
  feasible = function(selected, data)
    if data and #data == #selected then
      local areas = {}
      for _, id in ipairs(selected) do
        for _, v in ipairs(data) do
          if table.contains(v[2], id) then
            table.insertIfNeed(areas, v[2])
            break
          end
        end
      end
      return #areas == #selected
    end
  end,
  default_choice = function(data)
    if not data then return {} end
    local cids = table.map(data, function(v) return v[2][1] end)
    return cids
  end,
}

tunshi:addEffect(fk.EnterDying, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(tunshi.name) and player:usedSkillTimes(tunshi.name, Player.HistoryTurn) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player:isKongcheng() then
      player:showCards(player:getCardIds("h"))
      if player.dead then return end
    end
    local all_cards = table.filter(room:getBanner("@$CenterArea"), function (id)
      return not table.find(player:getCardIds("h"), function (id2)
        return Fk:getCardById(id):compareSuitWith(Fk:getCardById(id2))
      end)
    end)
    local cards = {}
    for _, id in ipairs(all_cards) do
      local suit = Fk:getCardById(id).suit
      cards[suit] = cards[suit] or {}
      table.insert(cards[suit], id)
    end
    local card_data = {}
    for suit, ids in pairs(cards) do
      table.insert(card_data, {U.ConvertSuit(suit, "int", "sym"), ids})
    end
    local ret = {}
    if table.every(card_data, function (info)
      return #info[2] == 1
    end) then
      for _, info in ipairs(card_data) do
        table.insert(ret, info[2][1])
      end
    else
      ret = room:askToPoxi(player, {
      poxi_type = tunshi.name,
      data = card_data,
      cancelable = false,
    })
    end
    room:moveCardTo(ret, Card.PlayerHand, player, fk.ReasonJustMove, tunshi.name, nil, true, player)
  end,
})

tunshi:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return tunshi
